$(function() {
    $('#orderform').live("mouseleave", Order.HiddenOrderForm);
    $('#orderform').live("mouseenter", Order.StopHiddenOrderForm);
    $('#clordform').live("click", Order.DoHideOrderForm);
    $('#sendorder').live("click", Order.PlaceOrder);

    $('#buy').live("click", Order.ChangeBuyBC);
    $('#sell').live("click", Order.ChangeSellBC);
    $('#marketprice').live("click", Order.ChangeToMarket);
    $('#limitprice').live("click", Order.ChangeToLimit);

});


var Order =
{
    OrderFormTimer: null
    , ChangeToLimit: function() {
        $('#priceask').removeAttr('disabled');
    }
    , ChangeToMarket: function() {
        $('#priceask').attr('disabled', 'disabled');
    }
    , ChangeBuyBC: function() {
        $('.placeorderarea').css('background-color', 'lightsalmon');
    }
    , ChangeSellBC: function() {
        $('.placeorderarea').css('background-color', 'lightGreen');
    }
    , GetOrderForm: function(memsn, e) {
        Order.StopHiddenOrderForm();
        var param = { sn: memsn };
        utility.service('RequestService/GetOrderForm', param, 'POST', function(data) {
            if (data.code > 0) {
                var templatepara = { 'pList': data.items, 'l': l, 'agsn': data.agsn };
                utility.template("Agent/BetOrder.html", function(template) {
                    $('#orderform').css('top', e.pageY + 10);
                    $('#orderform').css('left', e.pageX - 300);
                    $('#orderform').html(template.process(templatepara));
                    $('#orderform').show("slide", {}, 500);
                    $('#priceask').focus();
                }, "OrderForm");
            } else {
                utility.showPopUp(data.msg, 1);
            }
        });
    }
    , DoHideOrderForm: function() {
        if ($('#orderform').html().length > 0) {
            $('#orderform').hide("blind", {}, 500, function() { $('#orderform').html(''); });
        }
    }
    , HiddenOrderForm: function() {
        Order.OrderFormTimer = setTimeout('Order.DoHideOrderForm();', 1000);
    }
    , StopHiddenOrderForm: function() {
        clearTimeout(Order.OrderFormTimer);
    }
    , PlaceOrder: function() {
        targetFunction = (parent != undefined) ? parent.ErrorHelper : ErrorHelper;
        if ($('input[name=lm]:checked').val() == 'Limit') {
            targetFunction.checkNonNull($('#priceask'), m.Err0309);
        }

        if ($('input[name=buysell]:checked').val() == undefined) {
            targetFunction.setError(m.Err0302, $('input[name=buysell]'));
        }

        if (!targetFunction.popUpIfError()) {
            var param = $("#placeorderform").serialize();
            var cb = Order.HiddenOrderForm;
            var ecb = function() { $('#priceask').focus(); };
            utility.ajaxext("RequestService/PlaceRequestOrder", param, "POST", cb, ecb);
        }
    }
};
